package _04Set接口;

import java.util.*;

public class TreeSetDemo {
	public static void main(String[] args) {
		Set<Integer> s1 = new TreeSet();
		s1.add(100);
		s1.add(15);
		s1.add(10);
		s1.add(20);
		s1.add(10);
		s1.add(250);
		s1.add(150);
		s1.add(350);

		System.out.println(s1);

		Set<String> s2 = new TreeSet<>();
		s2.add("abc");
		s2.add("aac");
		s2.add("abb");
		s2.add("ac");
		s2.add("add");
		s2.add("abc");
		s2.add("bbc");
		s2.add("cbc");
		s2.add("dbc");
		System.out.println(s2);

		// 鑷畾涔夌殑绫�
		Set<Student2> s3 = new TreeSet<>();
		s3.add(new Student2(1, "寮犱笁", 18));
		s3.add(new Student2(10, "鍒樺", 15));
		s3.add(new Student2(5, "鍏充簬", 16));
		s3.add(new Student2(7, "寮犻", 17));
		s3.add(new Student2(20, "鏇规搷", 18));
		s3.add(new Student2(5, "瀛欐潈", 20));
		System.out.println(s3);
		// Exception in thread "main" java.lang.ClassCastException: _04Set鎺ュ彛.Student2
		// cannot be cast to java.lang.Comparable

		// 缁冧範:
		// 1. Student2鐨勪慨鏀规帓搴忚鍒�
		// 鏀逛负鎸夌収骞撮緞鎺掑簭

		// 2. 瀹氫箟涓�涓晢鍝佸垎绫荤被锛堝垎绫荤紪鍙� 锛屽垎绫诲悕)
		// 浣跨敤TreeSet瀛樻斁
		// 鏍规嵁鍒嗙被缂栧彿浠庡皬鍒板ぇ瀛樻斁
		Set<DevidedGoods> d = new TreeSet<>();
		d.add(new DevidedGoods(1023, "鏂囧叿绫�"));
		d.add(new DevidedGoods(253, "蹇绫�"));
		d.add(new DevidedGoods(589, "鏃ョ敤鍝�"));
		d.add(new DevidedGoods(124, "鐔熼"));
		d.add(new DevidedGoods(20, "闈㈤"));
		d.add(new DevidedGoods(5, "绱犻"));
		System.out.println(d);

		Set<Student> stus = new TreeSet<Student>(new Comparator<Student>() {

			@Override
			public int compare(Student o1, Student o2) {
				if (o1.age < o2.age) {
					return -1;

				} else if (o1.age == o2.age) {
					return 0;

				} else {
					return 0;
				}

			}
		});

		stus.add(new Student("寮犱笁", 18));
		stus.add(new Student("鏉庡洓", 16));
		stus.add(new Student("鐜嬩簲", 14));
		stus.add(new Student("璧靛叚", 17));
		stus.add(new Student("鐢颁竷", 12));
		System.out.println(stus);

	}

}
